package com.woniuxy.book.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.book.entity.PageInfo;
import com.woniuxy.book.entity.UserAddress;
import com.woniuxy.book.exception.useraddress.UserAddressException;
import com.woniuxy.book.exception.useraddress.UserAddressExceptionCode;
import com.woniuxy.book.mapper.UserAddressMapper;
import com.woniuxy.book.param.useraddress.AddAddressParam;
import com.woniuxy.book.param.useraddress.UpdateAddressParam;
import com.woniuxy.book.service.UserAddressService;
import com.woniuxy.book.util.StateData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author li
 * @since 2024年2月09日
 */
@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Resource
    private UserAddressMapper userAddressMapper;

    @Override
    public UserAddress findDefaultAddress(Integer userId) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("address_state", StateData.ADDRESS_STATE_DEFAULT);
        UserAddress userAddress = userAddressMapper.selectOne(queryWrapper);
        return userAddress;
    }

    @Override
    public void addAddress(AddAddressParam param) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",param.getUserId());
        List<UserAddress> userAddresses = userAddressMapper.selectList(queryWrapper);
        if (userAddresses==null||userAddresses.size()==0) {
            param.setAddressState(StateData.ADDRESS_STATE_DEFAULT);
        }else if (userAddresses!=null&&userAddresses.size()>0&&param.getAddressState()==StateData.ADDRESS_STATE_DEFAULT){
            for (UserAddress userAddress : userAddresses) {
                if (userAddress.getAddressState()== StateData.ADDRESS_STATE_DEFAULT) {
                    userAddress.setAddressState(StateData.ADDRESS_STATE_NORMAL);
                    userAddressMapper.updateById(userAddress);
                }
            }
        }else if(param.getAddressState()==null) {
            param.setAddressState(StateData.ADDRESS_STATE_NORMAL);
        }
        UserAddress userAddress = BeanUtil.toBean(param, UserAddress.class);
        userAddressMapper.insert(userAddress);

    }

    @Override
    public List<UserAddress> selectAddress(Integer userId) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.notIn("address_state",StateData.ADDRESS_STATE_DELETED);
        List<UserAddress> userAddresses = userAddressMapper.selectList(queryWrapper);
        return userAddresses;
    }

    @Override
    public UserAddress findAddress(Integer addressId, Integer userId) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address_id",addressId);
        queryWrapper.eq("user_id",userId);
        UserAddress userAddress = userAddressMapper.selectOne(queryWrapper);
        if (userAddress==null) {
            throw new UserAddressException(UserAddressExceptionCode.USER_ADDRESS_EXCEPTION_ADDRESS_NOTEXITS);
        }
        return userAddress;
    }

    @Override
    public PageInfo selectAddressPage(Integer pageNum, Integer pageSize, Integer userId) {
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.notIn("address_state",StateData.ADDRESS_STATE_DELETED);
        Page<UserAddress> page = new Page<UserAddress>(pageNum == null ? 1 : pageNum, pageSize == null ? 5 : pageSize);
        userAddressMapper.selectPage(page, wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(page.getRecords());
        return pageInfo;
    }

    @Override
    public void changeAddressState(Integer addressId, Integer addressState, Integer userId) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address_id",addressId);
        queryWrapper.eq("user_id",userId);
        UserAddress userAddress = userAddressMapper.selectOne(queryWrapper);
        if (userAddress==null) {
            throw new UserAddressException(UserAddressExceptionCode.USER_ADDRESS_EXCEPTION_ADDRESS_NOTEXITS);
        }
        if (userAddress.getAddressState()== StateData.ADDRESS_STATE_DEFAULT) {
            throw new UserAddressException(UserAddressExceptionCode.USER_ADDRESS_EXCEPTION_ADDRESS_DEFAULTED);
        }
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<UserAddress> userAddresses = userAddressMapper.selectList(wrapper);
        if (userAddresses==null||userAddresses.size()==0) {
            throw new UserAddressException(UserAddressExceptionCode.USER_ADDRESS_EXCEPTION_ADDRESS_NOTEXITS);
        }else if (userAddresses!=null&&userAddresses.size()>0&&addressState==StateData.ADDRESS_STATE_DEFAULT){
            for (UserAddress address : userAddresses) {
                if (address.getAddressState()== StateData.ADDRESS_STATE_DEFAULT) {
                    address.setAddressState(StateData.ADDRESS_STATE_NORMAL);
                    userAddressMapper.updateById(address);
                }
            }
        }
        userAddress.setAddressState(addressState);
        userAddressMapper.updateById(userAddress);
    }

    @Override
    public void updateAddress(UpdateAddressParam param) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address_id",param.getAddressId());
        queryWrapper.eq("user_id",param.getUserId());
        UserAddress userAddress = userAddressMapper.selectOne(queryWrapper);
        if (userAddress==null) {
            throw new UserAddressException(UserAddressExceptionCode.USER_ADDRESS_EXCEPTION_ADDRESS_NOTEXITS);
        }
        userAddress.setAddressInfo(param.getAddressInfo());
        userAddress.setReceiverName(param.getReceiverName());
        userAddress.setReceiverPhone(param.getReceiverPhone());
        userAddressMapper.updateById(userAddress);
    }

    @Override
    public void delAddress(Integer addressId, Integer userId) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address_id",addressId);
        queryWrapper.eq("user_id",userId);
        UserAddress userAddress = userAddressMapper.selectOne(queryWrapper);
        if (userAddress==null) {
            throw new UserAddressException(UserAddressExceptionCode.USER_ADDRESS_EXCEPTION_ADDRESS_NOTEXITS);
        }
        userAddress.setAddressState(StateData.ADDRESS_STATE_DELETED);
        userAddressMapper.updateById(userAddress);
    }
}
